<template>
  <div class="employees-detail-container">
    <el-card>
      <el-tabs>
        <el-tab-pane label="用户管理">
          <el-form ref="form" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
            <el-form-item label="姓名:" prop="username">
              <el-input v-model="userInfo.username" style="width:400px" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="密码:" prop="newPassword">
              <el-input v-model="userInfo.newPassword" style="width:400px" type="password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="savaUserInfo">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <UserInfo />
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <JobInfo />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { reqGetUserDetailById, reqSaveUserDetailById } from '@/api/user'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userInfo: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          { require: true, message: '请输入名称', trigger: ['blur', 'change'] }
        ],
        newPassword: [
          { min: 3, max: 7, message: '3-7位密码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  created() {
    this.getuserInfo()
  },
  methods: {
    async  getuserInfo() {
      const { data } = await reqGetUserDetailById(this.id)
      this.userInfo = data
    },
    savaUserInfo() {
      this.$refs.form.validate(async flag => {
        if (!flag) {
          this.$message.error('输入参数错误')
          return
        }
        const res = await reqSaveUserDetailById({
          ...this.userInfo,
          password: this.userInfo.newPassword
        })
        console.log(res)
        this.$message.success('保存成功')
      })
    }
  }
}
</script>

<style>

</style>
